﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminBannerController : Controller
    {
        //
        // GET: /AdminCP/AdminBanner/
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityBanners
                            select new AdminBannerModel
                            {
                                Id = p.Id,
                                Image = p.Image,
                                Link = p.Link,
                                Position = p.Position
                            };
                return View(query.ToList());
            }
        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityBanners
                            where p.Id==id
                            select new AdminBannerModel
                            {
                                Id = p.Id,
                                Image = p.Image,
                                Link = p.Link,
                                Position = p.Position
                            };
                if (query != null)
                    return View(query.FirstOrDefault());
                else
                    return View("Error");
            }
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AdminBannerModel model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityBanner banner = db.EntityBanners.FirstOrDefault(p => p.Id == model.Id);
                if (banner != null)
                {
                    banner.Link = model.Link;
                    banner.Position = model.Position;
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (banner.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/banner/"), banner.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/banner/"), encodestring);
                        Image.SaveAs(path);

                        banner.Image = encodestring;
                    }
                    db.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                    return View("Error");

                    

            }
        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            return View();

        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(AdminBannerModel model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityBanner banner = new EntityBanner();
                banner.Link = model.Link;
                    banner.Position = model.Position;
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (banner.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/banner/"), banner.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/banner/"), encodestring);
                        Image.SaveAs(path);

                        banner.Image = encodestring;
                    }
                    db.EntityBanners.InsertOnSubmit(banner);
                    db.SubmitChanges();
                    return RedirectToAction("Index");
                
            }
        }
        #endregion

        #region "Delete"
        public string Delete(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityBanner Banner = db.EntityBanners.FirstOrDefault(p => p.Id == id);
                if (Banner != null)
                {
                    if (Banner.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/banner/"), Banner.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }
                    db.EntityBanners.DeleteOnSubmit(Banner);
                    db.SubmitChanges();
                    return "Ok";
                }
                else
                    return "Error";
            }
        }
        #endregion
    }
}
